import { reqGetGoodsInfoData } from '@/api'

const state = {
  // 用来存放商品数据
  goodsInfo: {},
}
const actions = {
  // 1.发送请求获取商品的数据
  async getGoodsInfoData({ commit }, searchParams) {
    const result = await reqGetGoodsInfoData(searchParams)
    if (result.code === 200) {
      commit('SAVE_GOODS_INFO_DATA', result.data)
    } else {
      console.log(result.message)
    }
  },
}
const mutations = {
  // 1.将请求回来的数据存储起来
  SAVE_GOODS_INFO_DATA(state, goodsInfo) {
    state.goodsInfo = goodsInfo
  },
}
// getters 就相当于是Vuex中的计算属性
const getters = {
  // 1.商品列表
  goodsList(state) {
    return state.goodsInfo.goodsList
  },
  // 2.品牌列表
  trademarkList(state) {
    return state.goodsInfo.trademarkList
  },
  // 3.属性列表
  attrsList(state) {
    return state.goodsInfo.attrsList
  },
  // 4.商品总数量
  total(state) {
    return state.goodsInfo.total
  },
}

export default {
  namespaced: true, // 开启命名空间 区分不同的模块
  state,
  actions,
  mutations,
  getters,
}
